package com.deyuanyun.pic.eqpt.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.eqpt.domain.EquipmentInfo;
import com.deyuanyun.pic.eqpt.service.EquipmentInfoService;
import com.deyuanyun.pic.support.utils.ExcelUtils;
import com.github.pagehelper.PageHelper;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Bill.li on 2015/12/30.(设备管理)
 */
@Controller
@RequestMapping("/eqpt/equipment/equipmentInfo")
public class EquipmentInfoController extends EqptBaseController {
    @Resource
    private EquipmentInfoService equipmentInfoService;/*设备管理*/


    @RequestMapping(value="/equipmentInfoPage.html",method= RequestMethod.GET)
    public ModelAndView toInternallyPage( ModelAndView mv)
    {//主页
        mv.setViewName("/eqpt/equipmentInfoManagement");
        return mv;
    }

    @RequestMapping("/getQuipment.json")
    @ResponseBody
    public void getQuipment(HttpServletRequest request){
    	HashMap map=RequestUtil.parseRequestParamToHashMap(request);
        PageJsonBean jsonBean=new PageJsonBean();
        if(request.getParameter("jsonBean.pageNum") == null)
		{
			jsonBean = new PageJsonBean(false);
			PageHelper.startPage(1,1000);
		}
        List<EquipmentInfo> equipmentInfoList= equipmentInfoService.getQuipment(map);
        jsonBean.setList(equipmentInfoList);
        AjaxSupport.sendSuccessText("success",jsonBean);
    }
    @ResponseBody
    @RequestMapping(value="/addEquipmentInfo",method= RequestMethod.POST)
    public void addQuipment(EquipmentInfo info, HttpServletResponse response){
    	 ModelAndView mav = new ModelAndView();
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status= equipmentInfoService.addQuipment(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addObject("status", "1");
				mav.addAllObjects(param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }

    @ResponseBody
    @RequestMapping(value="/updateQuipment",method=RequestMethod.POST)
    public void updateQuipment(EquipmentInfo info, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
       
        
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status= equipmentInfoService.updateQuipment(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addObject("status", "1");
				mav.addAllObjects(param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }
    @ResponseBody
    @RequestMapping("/exportQuipment.json")
    public void exportExl(HttpServletRequest request, HttpServletResponse response){

        HashMap param=RequestUtil.parseRequestParamToHashMap(request);
        InputStream filePath =this.getClass().getResourceAsStream("/template/out/export_sb.xls");
        int[] lineStart = new int[] { 1 };/*从第二行开始写入数据!*/
        List<List<String[]>> result= (List<List<String[]>>) equipmentInfoService.exportQuipment(param);
        ExcelUtils.jxlExportExcel(filePath, lineStart, "设备管理", result, response);
    }
    @RequestMapping("/delQuipment.json")
    @ResponseBody
    public void delQuipment(HttpServletRequest request){
           

            HashMap data = RequestUtil.parseRequestParamToHashMap(request);
            ModelAndView mav = new ModelAndView();
            try {
    			boolean status=   equipmentInfoService.delQuipment(data);
    			if(status)
    			{
    				mav.addObject("status", "0");
    			}else{
    				mav.addObject("status", "1");
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    			mav.addObject("status", "1");
    		}
    		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }
}
